package com.apes.scm.account.ledger.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：往来总账条目
 *
 * @author xul
 * @create 2018-11-03 10:25
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = LedgerEntry.CONST_TABLE_NAME, indexes = {
        @Index(name = "i_acc_ledger_entry_id", columnList = "ledger_id"),
        @Index(name = "i_acc_ledger_entry_source", columnList = "source, source_business_type")})
public class LedgerEntry extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_ledger_entry";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "往来总账条目", padding = 10, prefix = "1")
    @Comment("编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ledger_id", referencedColumnName = "id", nullable = false)
    @Comment("合作伙伴往来总账")
    private Ledger ledger;

    @Column(name = "source", length = 30, nullable = false)
    @Comment("来源单号")
    private String source;

    @Column(name = "source_business_type", length = 32, nullable = false)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "source_business_type_name", length = 64)
    @Comment("来源单据类型名称")
    private String sourceBusinessTypeName;

    @Column(name = "origin", length = 30)
    @Comment("源单据")
    private String origin;

    @Column(name = "origin_business_type", length = 32)
    @Comment("源单据类型")
    private String originBusinessType;

    @Column(name = "origin_business_type_name", length = 64)
    @Comment("源单据类型名称")
    private String originBusinessTypeName;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Column(name = "accounting_date", nullable = false)
    @Comment("记帐日期")
    private Date accountingDate;

    @Column(name = "incurred", columnDefinition = ApesConst.FIELD_MONEY_16_4_0, nullable = false)
    @Comment("发生金额")
    private double incurred = 0;

    @Column(name = "amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总账金额")
    private double amount = 0;
}
